/*
 * Copyright (C) 2009 Rafael Fernandes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.phonebooksharing.android.activities.video;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.phonebooksharing.android.R;
import com.phonebooksharing.android.util.GalleryUtilities;
import com.phonebooksharing.android.widgets.drawable.FastBitmapDrawable;
import com.phonebooksharing.android.widgets.view.CrossableItem;
import com.phonebooksharing.dto.gallery.VideoGalleryItem;

public class VideoGalleryListAdapter extends BaseAdapter {
    public ArrayList<CrossableItem<VideoGalleryItem>> items;
    public ArrayList<Bitmap> thumbs;
    private LayoutInflater inflater;
//    private ListView lv;
    public View loading;
    public View.OnClickListener checkListener;
    
    public VideoGalleryListAdapter(Context cx, ListView lv) {
        this.items = new ArrayList<CrossableItem<VideoGalleryItem>>();
        this.inflater = LayoutInflater.from(cx);
//        this.lv = lv;
        loading = inflater.inflate(R.layout.loading, null, false);
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int location) {
        return items.get(location);
    }

    @Override
    public long getItemId(int location) {
        return items.get(location).item.videoID;
    }

    @Override
    public View getView(int position, View cv, ViewGroup arg2) {
        ViewHolder holder;
        if (cv == null) {
            cv = inflater.inflate(R.layout.list_item_3lines_check, null);

            holder = new ViewHolder();
            holder.icon = (ImageView) cv.findViewById(android.R.id.icon);
            holder.border = (View) cv.findViewById(android.R.id.background);
            holder.name = (TextView) cv.findViewById(R.id.name);
            holder.time = (TextView) cv.findViewById(R.id.date);
            holder.text1 = (TextView) cv.findViewById(R.id.title);
            holder.text2 = (TextView) cv.findViewById(R.id.text);
            
            holder.type = (ImageView) cv.findViewById(R.id.type);
            holder.check = (ImageView) cv.findViewById(R.id.check);
            
            holder.container = cv.findViewById(R.id.container);
            holder.container.setOnClickListener(checkListener);
            
            cv.setTag(holder);
        } else {
            holder = (ViewHolder) cv.getTag();
        }
        CrossableItem<VideoGalleryItem> cross = items.get(position);
        holder.container.setTag(cross);

        VideoGalleryItem video = cross.item;
        
        holder.id = video.videoID;
        holder.name.setText(video.title);
        holder.text1.setText(video.whereAt);
        holder.text2.setText(video.timesViewedString);
        holder.time.setText(video.videoLength);
        
//        holder.time.setText(DateUtils.formatElapsedTime(
//                (GeneralUtils.calculateGmtTimeInMilis(System.currentTimeMillis()) - 
//                 GeneralUtils.calculateGmtTimeInMilis(photo.when)) / 1000));
        
        FastBitmapDrawable f = GalleryUtilities.getFromVideoThumbCache(video.videoID);
        holder.icon.setImageBitmap((f != null ? f.getBitmap() : GalleryUtilities.loadingBitmap.getBitmap()));
        
//        if(TextUtils.isEmpty(video.photoThumbUri)) {
//            final FastBitmapDrawable icon = GalleryUtilities.getFromCache(video.videoID);
//            if(icon != null) {
//                holder.icon.setImageBitmap(icon.getBitmap());
//            } else {
//                holder.icon.setImageBitmap(GalleryUtilities.loadingBitmap.getBitmap());
//                //get video thumb
//                final String url = String.format("http:" + Global.BASE_URL + "/VideoServlet?ac=gvt&q=%s&w=%s&h=%s", video.videoID, 60, 60);
//                new LoadNetworkImageTask(lv, url).execute(video.videoID, 60, 60, position);
//            }
//        } else {
//            holder.icon.setImageURI(Uri.parse(video.photoThumbUri));
//        }
        
        return cv;
     }
     
    static class ViewHolder {
        ImageView icon, icon2, type, check;
        TextView name, time, text1, text2;
        View container, border;
        long id;
    }
}
